package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.model.DataSourceConfig;

import java.util.List;

/**
 * @author by PengHongChao
 * @date 2025/7/7 10:31
 */


@Mapper
public interface DataSourceConfigMapper {
    @Insert("INSERT INTO data_source_config(name, type, url, username, password, driver_class, enabled) " +
            "VALUES(#{name}, #{type}, #{url}, #{username}, #{password}, #{driverClass}, #{enabled})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(DataSourceConfig config);

    @Update("UPDATE data_source_config SET " +
            "name = #{name}, " +
            "type = #{type}, " +
            "url = #{url}, " +
            "username = #{username}, " +
            "password = #{password}, " +
            "driver_class = #{driverClass}, " +
            "enabled = #{enabled}, " +
            "updated_time = NOW() " +
            "WHERE id = #{id}")
    void update(DataSourceConfig config);

    @Delete("DELETE FROM data_source_config WHERE id = #{id}")
    void delete(Long id);

    @Select("SELECT * FROM data_source_config WHERE id = #{id}")
    DataSourceConfig selectById(Long id);

    @Select("SELECT * FROM data_source_config")
    List<DataSourceConfig> selectAll();

    @Select("SELECT * FROM data_source_config WHERE enabled = true")
    List<DataSourceConfig> selectAllEnabled();
}
